/*
 * @(#)SwingUtils.java	1.02 11/15/08
 *
 */
package org.tri.swing;

import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.Timer;

/**
 * A collection of utility methods for Swing.
 *
 * @author Darryl Burke
 */
public final class SwingUtils {

    private SwingUtils() {
        throw new Error( "SwingUtils is just a container for static methods" );
    }

    /**
     * Convenience method for searching below <code>container</code> in the
     * component hierarchy and return nested components that are instances of
     * class <code>clazz</code> it finds. Returns an empty list if no such
     * components exist in the container.
     * <P>
     * Invoking this method with a class parameter of JComponent.class
     * will return all nested components.
     * 
     * @param clazz the class of components whose instances are to be found.
     * @param container the container at which to begin the search
     * @param nested true to list components nested within another listed
     * component, false otherwise
     * @return the List of components
     */
    public static <T extends JComponent> List<T> getDescendantsOfType(
            Class<T> clazz, Container container, boolean nested ) {
        List<T> tList = new ArrayList<T>();
        for ( Component component : container.getComponents() ) {
            if ( clazz.isAssignableFrom( component.getClass() ) )
                tList.add( clazz.cast( component ) );
            if ( nested || !clazz.isAssignableFrom( component.getClass() ) )
                tList.addAll( SwingUtils.<T>getDescendantsOfType( clazz,
                                                                  (Container)component, nested ) );
        }
        return tList;
    }

    /**
     *  Convenience static method to disable all components of a given
     *  Container, including nested Containers.
     *
     *  @param container the Container containing Components to be disabled
     */
    public static void disable( Container container ) {
        List<JComponent> components =
                         SwingUtils.getDescendantsOfType( JComponent.class, container, true );

        for ( JComponent component : components )
            component.setEnabled( false );

        //deshabilitamos el container
        container.setEnabled( false );

    }

    /**
     *  Convenience static method to enable Components disabled by using
     *  the disable() method. Only Components disable by the disable()
     *  method will be enabled.
     *
     *  @param container a Container that has been previously disabled.
     */
    public static void enable( Container container ) {
        List<JComponent> components =
                         SwingUtils.getDescendantsOfType( JComponent.class, container, true );

        if ( components != null )
            for ( JComponent component : components )
                component.setEnabled( true );

        //habilitamos el container
        container.setEnabled( true );

    }

    public static void futureClearLabel( final JLabel label ) {
        Timer timer = new Timer( 5000, new ActionListener() {

            @Override
            public void actionPerformed( ActionEvent e ) {
                label.setText( "" );

            }

        } );

        timer.setRepeats( false );
        timer.start();

    }//end método clearLabel

}
